<include file="header" />
<body>
<div class="wraper">
	 <div class="load">
        <img class="load-bee" src="/assets/base/studentair/images/load-bee.png">

    </div>
	<div class="head clearfix">
		<a href="#" class="iconfont icon-zuo pull-left"></a>
		<span class="head-name pull-left">空调租赁</span>
	</div>
	<p class="form-list">您已经选择了：<span class="big-text">套餐{BEESCRM:$sort}</span></p>
	<p class="field">租赁内容</p>
	<div class="lists tb-mode">
		<div class="tb-cell user-info">
			<p>租赁期：<span class="price">{BEESCRM:$info.year}</span>年 <span class="price">[ {BEESCRM:$start_time} 至 {BEESCRM:$end_time} ]</span></p>
			<p>费用：{BEESCRM:$info.money}元/台</p>
			<p>押金：{BEESCRM:$info.deposit}元/台</p>
			<p>现支付：<span class="price">{BEESCRM:$info['deposit']}</span>元（租期满后，如无损坏，扣除费用，余款退回）</p>
		</div>
	</div>
	<div class="lists tb-mode">
		<div class="tb-cell user-info">
			<p class="name">{BEESCRM:$user.community_name}{BEESCRM:$user.building_name}{BEESCRM:$user.room}&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-gray">安装寝室</span></p>
			<p>申请人：{BEESCRM:$user.username}</p>
			<p>学号：{BEESCRM:$user.student_number}</p>
			<p>学校：{BEESCRM:$user.school}</p>
			<p class="phone">手机：<input value="{BEESCRM:$user.mobile}" id="stumobile"></p>
		</div>
		<!--<div class="tb-cell tb-btn">
			<a href="#"class="btn">编辑个人信息</a>
		</div>-->
	</div>
	<p class="field">请填写室友信息</p>
	<div class="form-box">
		<div class="line clearfix">
			<p class="title">姓名</p>
			<p class="title">学号</p>
			<p class="title">手机</p>
		</div>
		<for start="1" end="$sum">
		<div class="line clearfix">
			<input class="title username" value="">
			<input class="title studentnum" value="">
			<input class="title mobile" value="">
		</div>
		</for>
	</div>
	<p class="block-space xieyi"><span class="iconfont icon-checkbox" data-id="1"></span>我已阅读并同意<a href="javascript:void(0);" class="blue xieyisee">《空调租赁协议》</a></p>
	<div class="btn-box clearfix">
		<input type="hidden" value="{BEESCRM:$order_id}" id="order_id">
		<input type="hidden" value="{BEESCRM:$user.community_name}" id="community_name">
		<input type="hidden" value="{BEESCRM:$user.building_name}" id="building_name">
		<input type="hidden" value="{BEESCRM:$user.room}" id="room">
		<input type="hidden" value="{BEESCRM:$user.username}" id="user">
		<input type="hidden" value="{BEESCRM:$user.student_number}" id="usernum">
		<input type="hidden" value="{BEESCRM:$sum}" id="sum">
		<a href="javascript:void(0);" class="btn half-btn save">发起租赁</a>
		<a href="{BEESCRM::U('base/studentAir/index',array('wechat_id'=>$wechat_id,'sign' => 1))}" class="btn half-btn">重新发起</a>
	</div>

	<div class="bottom-box">
		<p class="copyright">青橄榄网络 技术支持</p>
	</div>
</div>
<div class="pop-box showpay">
	<div class="main">
		<p class="close"><span class="iconfont icon-guanbi"></span></p>
		<div class="lists tb-mode text-left">
			<div class="tb-cell user-info">
				<p>安装地址：{BEESCRM:$user.school} {BEESCRM:$user.community_name}{BEESCRM:$user.building_name}{BEESCRM:$user.room}</p>
				<p>租赁期：<span class="price">{BEESCRM:$info.year}</span>年 <span class="price">[ {BEESCRM:$start_time} 至 {BEESCRM:$end_time} ]</span></p>
				<p>费用：{BEESCRM:$info.money}元/台</p>
				<p>押金：{BEESCRM:$info.deposit}元/台</p>
				<p>现支付：<span class="price">{BEESCRM:$info['deposit']}</span>元</p>
			</div>
		</div>
		<div class="pop-btn"><a href="javascript:void(0);" class="btn pay">去付款</a></div>
	</div>
</div>

<div class="pop-box protocol">
	<p class="close"><span class="iconfont icon-guanbi"></span></p>
	<div class="text-main">
		<p>1. 申请发起后，需所有室友在有申请效期内确认同意，租赁申请视为发起成功，可进入付款，付款后安装人员上门安装。如有1位或以上室友不同意该申请，则租赁申请发起失败。</p>
		<p>2. 申请发起后，需所有室友在有申请效期内确认同意，租赁申请视为发起成功，可进入付款，付款后安装人员上门安装。如有1位或以上室友不同意该申请，则租赁申请发起失败。</p>
		<p>3. 申请发起后，需所有室友在有申请效期内确认同意，租赁申请视为发起成功，可进入付款，付款后安装人员上门安装。如有1位或以上室友不同意该申请，则租赁申</p>
	</div>
	<div class="btn-box">
		<a href="#" class="btn" id="back">返回</a>
	</div>
</div>

<include file="commonJs" />
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
	var height = $(window).height();
	var bottombox_height = $('.bottom-box').outerHeight();
	$('.wraper').css("minHeight",height);
	$('.wraper').css("paddingBottom",bottombox_height);

	$(window).load(function(){
		$('.load').hide();
	});
	$(function(){
		$('.xieyi').click(function(){
			$(this).find('.iconfont').toggleClass('icon-xuanzedianjihou');
		})
	});

	$('.xieyisee').on('click',function(){
		$('.protocol').show();
	});

	$('.icon-guanbi').on('click',function(){
		$('.pop-box').hide();
	});

	$('#back').on('click',function(){
		$('.protocol').hide();
	});
	/*发起租赁，提交数据，添加宿舍参与人*/
	$('.save').on('click',function(){
		var community_name = $('#community_name').val();
		var order_id = $('#order_id').val();
		var building_name = $('#building_name').val();
		var room = $('#room').val();
		var usernum = $('#usernum').val();
		var sum = $('#sum').val();
		var username = {};
		var studentnum = {};
		var mobile = {};
		var stu_mobile = $('#stumobile').val();
		if(!stu_mobile){
			$.alert('error','申请人请填写手机号码');
			return false;
		}else{
			var reg = /^(?:13\d|15\d|18\d|17\d|14\d)\d{5}(\d{3}|\*{3})$/;
			if(!reg.test(stu_mobile)){
				$.alert('error','申请人电话格式不正确');
				return false;
			}
		}
		var bool = check();

		function check() {
			var k = 0;
			$('.mobile').each(function (i) {
				mobile[i] = $(this).val();
				if (!mobile[i]) {
					$.alert('error', '室友' + (i + 1) + '电话不能为空');
					k++;
					return false;
				} else {
					var reg = /^(?:13\d|15\d|18\d|17\d|14\d)\d{5}(\d{3}|\*{3})$/;
					if (!reg.test(mobile[i])) {
						$.alert('error', '室友' + (i + 1) + '电话格式不正确');
						k++;
						return false;
					}
				}
			});

			$('.studentnum').each(function (i) {
				studentnum[i] = $(this).val();
				if (!studentnum[i]) {
					$.alert('error', '室友' + (i + 1) + '学号不能为空');
					k++;
					return false;
				} else {
					var reg = /\d/;
					if (!reg.test(studentnum[i])) {
						$.alert('error', '室友' + (i + 1) + '学号格式不正确');
						k++;
						return false;
					}
				}
			});

			$('.username').each(function (i) {
				username[i] = $(this).val();
				if (!username[i]) {
					$.alert('error', '室友' + (i + 1) + '姓名不能为空');
					k++;
					return false;
				}
			});

			if(k == 0){
				return true;
			}else{
				return false;
			}
		}

		if(bool){
			var id = $('.icon-xuanzedianjihou').data('id');
			if(id != 1){
				$.alert('error','请阅读并同意租赁协议');
				return false;
			}

			var data = {};
			data.community_name = community_name;
			data.order_id = order_id;
			data.building_name = building_name;
			data.stu_mobile = stu_mobile;
			data.room = room;
			data.sum = sum;
			data.usernum = usernum;
			data.username = username;
			data.studentnum = studentnum;
			data.mobile = mobile;

			$.ajax({
				'url': "/base/studentAir/addUser/wechat_id/{BEESCRM:$wechat_id}",
				'data': data,
				'dataType':'json',
				'type': 'POST',
				'async': false,
				success: function(data) {
					if (data.status == 'S') { //添加成功
						$('.showpay').show();
					}else{
						$.alert('error',data.message);
						return false;
					}
				}
			});
		}
	});

	//提交订单
	$(document).on('click','.pay',function(){
		var order_id = $('#order_id').val();

		if(!order_id){
			$.alert('error','参数错误');
			return false;
		}

		$.ajax({
			'url': "/base/studentAir/orderPay?wechat_id={BEESCRM:$wechat_id}",
			'data': {'order_id':order_id},
			'dataType':'json',
			'type': 'POST',
			'async': false,
			success: function(data) {
				if (data.status == 'S') {
					var wxpay = $.parseJSON(data.wxpay);
					var order_id = data.order_id;
					pay(wxpay,order_id);
				}else{
					$.alert('error',data.message);
					return false;
				}
			}
		});
	});


	//微支付
	function pay(wxpay,id){
		if (typeof WeixinJSBridge == "undefined"){
			if( document.addEventListener ){
				document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			}else if (document.attachEvent){
				document.attachEvent('WeixinJSBridgeReady', jsApiCall);
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			}
		}else{
			WeixinJSBridge.invoke(
					'getBrandWCPayRequest',
					wxpay,
					function(res){
						WeixinJSBridge.log(res.err_msg);
						if(res.err_msg == "get_brand_wcpay_request:ok"){
							paySuccess(id);
						}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
							$.alert('error','付款已取消');
							return false;
						}else{
							$.alert('error','调用微支付失败');
							return false;
						}
					}
			);
		}
	}

	function paySuccess(order_id){
		var json = {};
		json.url = "/base/studentAir/paysuccess?wechat_id={BEESCRM:$wechat_id}&order_id="+order_id;
		$.getAjax(json,false,redirect,'');
	}

	function redirect(data){
		location.href = data.url;
	}
</script>
</body>
</html>
